﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class AddOrderForm : Form
    {
        private OrderDTO _dto = new OrderDTO();
        private OrderInfoBLL _orderInfoBLL = new OrderInfoBLL();
        private int _roomId;
        private string _name;

        public AddOrderForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }



        public void GetValue(object obj, EventArgs e)
        {

            Console.WriteLine("====================");
            _dto = e as OrderDTO;

            label6.Text = _dto.RoomName;
            label7.Text = _dto.DeskName;
            label10.Text = _dto.RoomMinConsumption.ToString();

        }

        public event EventHandler OrderAddProductsEvent;

        private void btnOK_Click(object sender, EventArgs e)
        {
            //开单后添加客户的订单信息
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.SubTime = DateTime.Now;
            orderInfo.Remark = txtDescription.Text;
            orderInfo.NumberOfDiners = Convert.ToInt32(txtPersonCount.Text);

            Console.WriteLine("================================");
            Console.WriteLine(orderInfo.SubTime);
            Console.WriteLine(orderInfo.Remark);
            Console.WriteLine(orderInfo.NumberOfDiners);

            int orderId = _orderInfoBLL.AddOrderInfo(orderInfo);

            //开单后添加订单和餐桌的使用情况信息
            int deskId = _dto.DeskId;
            int v = _orderInfoBLL.AddOrderAndDeskInfo(orderId, deskId);


            //修改餐桌表的状态信息
            DeskInfoBLL deskInfoBLL = new DeskInfoBLL();
            int v1 = deskInfoBLL.UpdateDeskState(deskId,1);

            if(v + v1 > 1)
            {
                MessageBox.Show("订单添加成功");
                //关闭窗口刷新桌子信息
                this.DialogResult = DialogResult.OK;

                if (ckbMeal.Checked)
                {
                    OrderAddProductsForm orderAddProductsForm = new OrderAddProductsForm();
                    _dto.OrderId = orderId;

                    OrderAddProductsEvent += orderAddProductsForm.GetData;
                    OrderAddProductsEvent.Invoke(this, _dto);
                    orderAddProductsForm.ShowDialog();
                    OrderAddProductsEvent -= orderAddProductsForm.GetData;
                }
            }
            else
            {
                MessageBox.Show("订单添加失败");
            }
        }

        
    }



}
